package com.sxc.study;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

/**
 * Create by sunxunchao on 2019/9/18
 */
public class AlarmBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "AlarmBroadcastReceiver";
    public static final String ACTION_ALARM = "action_alarm";

    private OnAlarmListener onAlarmListener;

    public AlarmBroadcastReceiver(OnAlarmListener onAlarmListener) {
        this.onAlarmListener = onAlarmListener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (ACTION_ALARM.equals(intent.getAction())) {
            if (onAlarmListener != null) {
                onAlarmListener.onAlarmed();
            }
            Log.d(TAG, "onReceive: alarm");
        }
    }


    public static void registerReceiver(Context context, AlarmBroadcastReceiver receiver) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_ALARM);
        context.registerReceiver(receiver, filter);
    }

    public static void unregisterReceiver(Context context, AlarmBroadcastReceiver receiver) {
        context.unregisterReceiver(receiver);
    }

    public interface OnAlarmListener {
        void onAlarmed();
    }
}
